package com.commonlover.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.commonlover.mapper.ArticleMapper;
import com.commonlover.result.PageVo;
import com.commonlover.service.ArticleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedHashMap;
import java.util.List;

@Service
@Transactional
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public PageVo<LinkedHashMap<String,Object>> findArticleByAuthorIdAndPageParam(JSONObject param) {
        PageHelper.startPage(param.getInteger("pageNum"), param.getInteger("pageSize"), true);
        List<LinkedHashMap<String, Object>> results = articleMapper.findArticleByAuthorId(param.getInteger("authorId"));
        PageInfo<LinkedHashMap<String, Object>> pageInfo = new PageInfo<>(results);
        return PageVo.<LinkedHashMap<String, Object>>builder()
                .page(param.getInteger("pageNum"))
                .size(param.getInteger("pageSize"))
                .total(pageInfo.getTotal())
                .values(results)
                .build();
    }

}